<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryList" :inline="true" label-width="95px">
        <el-form-item label="生产商名称">
          <el-select v-model="queryList.name" placeholder="请输入生产商名称" clearable :style="{ width: '100%' }" remote
            :remote-method="searchSelectNccUserCode" :loading="loading" filterable>
            <el-option v-for="item in pronameOptions"  :label="item.label" :value="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="生产商代码">
          <el-input v-model="queryList.code" placeholder="请输入生产商代码" clearable style="width: 150px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" v-hasPermi="['basic:nccBasic:add']" @click="addDialog('add')">新增</el-button>
          <el-button type="primary" icon="el-icon-search" v-hasPermi="['basic:nccBasic:query']" @click="filterDire">查询</el-button>
          <el-button type="danger" icon="el-icon-refresh" @click="clearAll">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="producerList" border height="605px">
        <el-table-column type="index" label="序号" align="center"> </el-table-column>
        <el-table-column prop="code" label="生产商代码" align="center"></el-table-column>
        <el-table-column prop="name" label="生产商名称" align="center"></el-table-column>
        <el-table-column prop="creationtime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="是否启用" align="center">
          <template slot-scope="scope">
            <el-switch :value="scope.row.enablestate" :active-value="'2'" :inactive-value="'1'" active-color="#13ce66"
              v-hasPermi="['basic:nccBasic:stateEdit']"  inactive-color="#ff4949" @change="toggleState(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="addDialog('edit',scope.row)"
             v-hasPermi="['basic:nccBasic:edit']" :disabled="scope.row.enablestate==='1'">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="total, prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="queryList.pageNum" :page-size="queryList.pageSize"
        :page-sizes="[10, 20, 50,100]" :total="total">
      </el-pagination>
    </el-card>
    <producerAdd ref="producerAddRef" :visible.sync="visible" @addSuccess="addSuccess"/>
  </div>
</template>

<script>
  import producerAdd from "@/components/BasicInformation/producerAdd.vue"
  import {
    findNccMaterialManufacturerData,
    nccMaterialManufacturerEnableStatusUpdate
  } from '@/api/basic/PersonnelInformation.js'
import { getSelectNccManufacturerInfo } from '@/api/dropDown.js'
  export default {
    components: {
      producerAdd
    },
    data() {
      return {
        queryList: {
          name: '', //生产商名称
          code: '', //生产商编号
        },
        producerList: [], //el-table数据
        total: 0,
        visible: false,
        pronameOptions:[],
        loading:false
      };
    },
    created() {
      this.getNccProducer();
    },
    methods: {
       /**
    * 远程搜索函数
    * 获取NCC用户信息-code
    */
    searchSelectNccUserCode(query) {
      if (query !== '') {
        this.loading = true; // 开始加载
        const params = {
          manufacturerName: query, // 传入输入的关键字
        };
        // 调用接口进行用户信息查询
        getSelectNccManufacturerInfo(params)
          .then(response => {
            if (response.code === 200) {
              // 更新物料选项
              this.pronameOptions = response.data.map(item => ({
                value: item.value, // 用户主键
                label: item.label, // 用户名称
              }));

              this.loading = false; // 开始加载
            }
          })
      }
    },
      getNccProducer() {
        findNccMaterialManufacturerData(this.queryList).then(response => {
          this.producerList = response.rows;
          this.total = response.total;
        });
      },
      //新增/修改后刷新列表
      addSuccess(){
        this.getNccProducer();
      },
      clearAll() {
        this.queryList = {
          nccStoName: "",
          nccStoCode: null,
        };
        this.getNccProducer();
      },
      filterDire() {
        this.getNccProducer()
      },
      // 任务状态修改
      toggleState(row) {
        // 根据 enablestate 判断是启用还是停用
        let text = row.enablestate === "2" ? "停用" : "启用";
        let query = {
          status: row.enablestate === "2" ? "1" : "2", // 状态切换：如果是 "2"（禁用），则切换为 "1"（启用）；反之亦然
          code: row.code
        };
        // 弹出确认框
        this.$confirm(`确认要${text}此项任务吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 更新状态
          nccMaterialManufacturerEnableStatusUpdate(query).then(res => {
            // 显示成功消息
            this.$message({
              type: 'success',
              message: `${text}成功!` // 根据操作状态来动态显示消息
            });
            // 更新列表或者执行其他操作（例如刷新数据等）
            this.getNccProducer(); // 这里假设有一个方法用于刷新列表数据
          }).catch(error => {
            // 错误处理
            this.$message({
              type: 'error',
              message: `操作失败: ${error.message || '未知错误'}` // 显示错误消息
            });
          });
        }).catch(() => {
          // 用户取消了操作
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      },

      addDialog(type, row) {
        this.visible = true;
        this.$refs.producerAddRef.openDialog(type, row);
      },
      handleSizeChange(newsize) {
        this.queryList.pageNum = 1
        this.queryList.pageSize = newsize
        this.getNccProducer()
      },
      handleCurrentChange(newpage) {
        this.queryList.pageNum = newpage
        this.getNccProducer()
      },
    }
  };
</script>

<style scoped>
  .app-container {
    padding: 20px;
  }

  .el-card {
    margin-bottom: 20px;
  }

  .btn el-button {
    margin-bottom: 20px;
  }

  .el-pagination {
    float: right;
    margin: 20px;
  }
</style>
